const { PrefabCache } = require("../util/gameResources");
let canShow = true;
let canMsgShow = true;
let tipAlert = {
    _alert: null,           //prefab
    _animSpeed: 0.3,        //弹窗动画速度
};

let show = function (tipStr,message,callback) {
    let prefab = PrefabCache['tipAlert'];
    if( !prefab){
        prefab = PrefabCache['toast'];
    }
    let prefabNode = cc.instantiate(prefab);
    
    if(!canShow){
        return ;
    }
    canShow = false;
    tipAlert._alert = prefabNode;
    cc.director.getScene().addChild(tipAlert._alert,3);
    tipAlert._alert.zIndex = 100;
    cc.find("tipAlert/tips").getComponent(cc.Label).string = tipStr;
    cc.find("tipAlert/message").getComponent(cc.Label).string = message;
    // cc.find("tipAlert/content/bottom/left_btn/leftbtn").getComponent(cc.Label).string = leftStr;
    // cc.find("tipAlert/content/bottom/right_btn/rightbtn").getComponent(cc.Label).string = rightStr;
    if(callback){
        cc.find("tipAlert/ensure").on('click', function (event) {
            dismiss();
            callback(true);
        }, this);

        cc.find("tipAlert/cancel").on('click', function (event) {
            dismiss();
            callback(false);
        }, this);
    }else{
        setTimeout(()=>{
            dismiss();
        },1000)
    }
    //设置parent 为当前场景的Canvas ，position跟随父节点
    tipAlert._alert.parent = cc.find("Canvas");
    startFadeIn();

};

let msg =async function (tipStr) {
    if(!canMsgShow) {
        return ;
    }
    canMsgShow = false;
    let prefab = PrefabCache['msg'];
    if(!prefab) {
        prefab =await cc.loader.loadRes("prefab/msg");
    }
    let msg = cc.instantiate(prefab);
    // cc.director.getScene().addChild(tipAlert._alert1,3);
    msg.zIndex = 100;
    msg.getChildByName('tips').getComponent(cc.Label).string = tipStr;
    // cc.find("tipAlert/content/bottom/left_btn/leftbtn").getComponent(cc.Label).string = leftStr;
    // cc.find("tipAlert/content/bottom/right_btn/rightbtn").getComponent(cc.Label).string = rightStr;
        setTimeout(()=>{
            canMsgShow = true;
            if(cc.isValid(msg)){
                msg.destroy();
            }
        },1500)
    //设置parent 为当前场景的Canvas ，position跟随父节点
    msg.parent = cc.find("Canvas");
    msgFadeIn(msg);
};
let msgFadeIn = function(msg) {
    msg.setScale(2);
    msg.opacity = 0;
    let cbFadeIn = cc.callFunc(onFadeInFinish, this);
    let actionFadeIn = cc.sequence(cc.spawn(cc.fadeTo(tipAlert._animSpeed, 255), cc.scaleTo(tipAlert._animSpeed, 1.0)), cbFadeIn);
    msg.runAction(actionFadeIn);
};

// 执行弹进动画
let startFadeIn = function () {
    //动画
    tipAlert._alert.setScale(2);
    tipAlert._alert.opacity = 0;
    let cbFadeIn = cc.callFunc(onFadeInFinish, this);
    let actionFadeIn = cc.sequence(cc.spawn(cc.fadeTo(tipAlert._animSpeed, 255), cc.scaleTo(tipAlert._animSpeed, 1.0)), cbFadeIn);
    tipAlert._alert.runAction(actionFadeIn);
};
 
 
// 弹进动画完成回调
let onFadeInFinish = function () {

};
 
// 执行弹出动画
let dismiss = function () {
    if (!tipAlert._alert) {
        return;
    }
    let cbFadeOut = cc.callFunc(onFadeOutFinish, this);
    let actionFadeOut = cc.sequence(cc.spawn(cc.fadeTo(tipAlert._animSpeed, 0), cc.scaleTo(tipAlert._animSpeed, 2.0)), cbFadeOut);
    tipAlert._alert.runAction(actionFadeOut);
};
 
// 弹出动画完成回调
let onFadeOutFinish = function () {
    onDestroy();
};
 
let onDestroy = function () {
    if (tipAlert._alert != null) {
        tipAlert._alert.removeFromParent();
        canShow = true;
        tipAlert._alert = null;
    }
};
 
module.exports={
  show,
  msg,
};